package mvv.learn.spring.services.elastic;

import java.util.LinkedList;
import java.util.List;

import mvv.learn.spring.dao.elasticsearch.ElasticsearchConstant;
import mvv.learn.spring.dao.elasticsearch.ElasticsearchDao;
import mvv.learn.spring.dto.elasticsearch.ElsEmployeeDto;
import mvv.learn.spring.dto.elasticsearch.ElsIndexingResponse;
import mvv.learn.spring.dto.response.ResResponse;
import mvv.learn.spring.services.accessdata.EmployeeService;
import mvv.learn.spring.utitilies.AppParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Manh Vu
 */
@Service
public class IndexElasticsearchService {

	@Autowired
	private EmployeeService employeeService;

	@Autowired
	private ElasticsearchDao elasticsearchDao;

	/**
	 * @author Manh Vu
	 * @return
	 */
	public ResResponse indexEmployee() {
		List<ElsEmployeeDto> list = employeeService.getElsEmployeeDtos(null);

		String baseUrl = elasticsearchDao.getBaseUrl();
		String urlIndex = baseUrl + AppParam.VAL_SLASH + ElasticsearchConstant.I_EMPLOYEE;

		// create index
		elasticsearchDao.createIndex(urlIndex);

		List<ElsIndexingResponse> indexReport = new LinkedList<>();
		String urlType = urlIndex + AppParam.VAL_SLASH + ElasticsearchConstant.T_EMPLOYEE;
		for (ElsEmployeeDto dto : list) {
			ElsIndexingResponse res = elasticsearchDao.indexDocument(dto, urlType);
			indexReport.add(res);
		}

		return (new ResResponse(AppParam.VAL_GOOD, null, indexReport));
	}

}
